package com.geeklan.smart.ui.pet.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.geeklan.smart.widget.ItemOnClickListener;
import com.geeklan.smart.databinding.ItemPetImageBinding;
import com.geeklan.smart.utils.FileUtils;

import java.util.ArrayList;
import java.util.List;

public class PetAdapter extends RecyclerView.Adapter<PetAdapter.ViewHolder> implements Filterable {

    private List<String> petImages;
    private List<String> filteredList = new ArrayList<>();
    private final Context context;
    private ItemOnClickListener itemOnClickListener;

    public void setItemOnClickListener(ItemOnClickListener itemOnClickListener) {
        this.itemOnClickListener = itemOnClickListener;
    }

    public void setData(List<String> petImages){
        this.petImages = petImages;
        this.filteredList.clear();
        this.filteredList.addAll(petImages);
        notifyDataSetChanged();
    }

    public PetAdapter(Context context) {
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new PetAdapter.ViewHolder(ItemPetImageBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        String petImage = filteredList.get(position);
        holder.mBinding.tvPetName.setText(FileUtils.extractFileNameWithoutExtension(petImage));
        Glide.with(context).load(petImage).into(holder.mBinding.imageView);
        holder.mBinding.getRoot().setOnClickListener(v -> {
            itemOnClickListener.OnItemClick(v,position);
        });
    }

    @Override
    public int getItemCount() {
        return filteredList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ItemPetImageBinding mBinding;

        public ViewHolder(@NonNull ItemPetImageBinding mBinding) {
            super(mBinding.getRoot());
            this.mBinding = mBinding;
        }
    }

    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                List<String> filtered = new ArrayList<>();
                if (constraint == null || constraint.length() == 0) {
                    filtered.addAll(petImages);
                } else {
                    String filterPattern = constraint.toString().toLowerCase().trim();
                    for (String item : petImages) {
                        if (item.toLowerCase().contains(filterPattern)) {
                            filtered.add(item);
                        }
                    }
                }

                FilterResults results = new FilterResults();
                results.values = filtered;
                results.count = filtered.size();
                return results;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                filteredList.clear();
                filteredList.addAll((List<String>) results.values);
                notifyDataSetChanged();
            }
        };
    }
}